package com.example.demo.filter;


import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.spi.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

import ch.qos.logback.classic.Logger;



/**
 * api网关权限过滤器
 * @author shenxiaodan
 *
 */
@Component
public class AccessFilter extends ZuulFilter {
	private static org.slf4j.Logger log =  org.slf4j.LoggerFactory.getLogger(AccessFilter.class);
	

	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		log.info("send {} request to {}", request.getMethod(),request.getRequestURL());
		String accessToken = request.getParameter("accessToken");
		if(accessToken==null){
			log.warn("access token is empty!");
			ctx.setSendZuulResponse(false); //不对此次请求路由
			ctx.setResponseStatusCode(401);
			return null;
		}
		
		return null;
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public int filterOrder() {
		return 0;
	}

	@Override
	public String filterType() {
		//请求路由之前执行
		return "pre";
	}

}
